package com.zlll.winner.controller.bbs;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zlll.winner.LoginUser;
import com.zlll.winner.business.model.bbs.BbsPost;
import com.zlll.winner.business.model.bbs.BbsPostMedia;
import com.zlll.winner.business.model.lhtUser.LhtUser;
import com.zlll.winner.business.service.bbs.IBbsPostMediaService;
import com.zlll.winner.business.service.bbs.IBbsPostService;
import com.zlll.winner.business.service.lhtUser.ILhtUserService;
import com.zlll.winner.business.vo.bbs.BbsPostVo;
import com.zlll.winner.common.BaseController;
import com.zlll.winner.enums.CommonEnum;
import com.zlll.winner.enums.bbs.PostEnum;
import javafx.geometry.Pos;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 论坛专题管理
 */
@LoginUser
@RestController
@RequestMapping(value = "/bbs/post")
public class BbsPostController extends BaseController {

    @Reference
    private IBbsPostService bbsPostService;

    @Reference
    private IBbsPostMediaService bbsPostMediaService;

    @Reference
    private ILhtUserService lhtUserService;

    /**
     * 分页列表
     * @param params
     * @return
     */
    @PostMapping(value = "page")
    public Object page(@RequestBody JSONObject params){
        Integer pageNo = params.getInteger("pageNo");
        Integer pageSize = params.getInteger("pageSize");
        Assert.notNull(pageNo,"pageNo不能为空");
        Assert.notNull(pageSize,"pageSize不能为空");
        return bbsPostService.fetchPostPage(params);
    }

    /**
     * 保存
     * @param params
     * @return
     */
    @PostMapping(value = "save")
    public Object save(@RequestBody JSONObject params) throws InterruptedException {
        BbsPost bbsPost = params.toJavaObject(BbsPost.class);
        return bbsPostService.save(bbsPost,params);
    }

    /**
     * 更新
     * @param params
     * @return
     */
    @PostMapping(value = "update")
    public Object update(@RequestBody JSONObject params){
        String actionType = params.getString("actionType");
        Long id = params.getLong("id");
        BbsPost updateObj = new BbsPost();
        updateObj.setUpdateTime(System.currentTimeMillis());
        updateObj.setId(id);
        if(actionType!= null && "pass".equals(actionType)){
            updateObj.setAuditStatus(PostEnum.AuditStatusEnum.PASS.val());
        }else if(actionType!= null && "noPass".equals(actionType)){
            String noPassReason = params.getString("noPassReason");
            updateObj.setAuditStatus(PostEnum.AuditStatusEnum.NO_PASS.val());
            updateObj.setAuditNoPassReason(noPassReason);
        }else if(actionType!= null && "putDown".equals(actionType)){
            updateObj.setStatus(PostEnum.StatusEnum.PUT_BACK.val());
            //维权处理 同事更新到维权状态
            updateObj.setComplaintStatus(PostEnum.SafeGuardStatusEnum.DOWN.val());
        }else if(actionType!= null && "follow".equals(actionType)){
            updateObj.setStatus(PostEnum.StatusEnum.PUT_AWAY.val());
            updateObj.setComplaintStatus(PostEnum.SafeGuardStatusEnum.FOLLOW.val());
        }else if(actionType!= null && "slove".equals(actionType)){
            updateObj.setStatus(PostEnum.StatusEnum.PUT_AWAY.val());
            updateObj.setComplaintStatus(PostEnum.SafeGuardStatusEnum.SLOVE.val());
        }else if(actionType!= null && "reply".equals(actionType)){
            updateObj.setStatus(PostEnum.StatusEnum.PUT_AWAY.val());
            updateObj.setComplaintStatus(PostEnum.SafeGuardStatusEnum.REPLY.val());
        }else if(actionType!= null && "passed".equals(actionType)){
            updateObj.setStatus(PostEnum.StatusEnum.PUT_AWAY.val());
            updateObj.setComplaintStatus(PostEnum.SafeGuardStatusEnum.PASSED.val());
        }
        boolean b = bbsPostService.updateById(updateObj);
        return updateObj;
    }

    /**
     * 删除
     * @param params
     * @return
     */
    @PostMapping(value = "delete")
    public Object delete(@RequestBody JSONObject params){
        Long id = params.getLong("id");
        Assert.notNull(id,"记录Id不能为空");
        BbsPost updateObj = new BbsPost();
        updateObj.setId(id);
        updateObj.setUpdateTime(System.currentTimeMillis());
        updateObj.setIsDel(CommonEnum.DelEnum.DEL.val());
        return bbsPostService.updateById(updateObj);
    }

    /**
     * 详情
     * @param params
     * @return
     */
    @PostMapping(value = "details")
    public Object details(@RequestBody JSONObject params){
        Long id = params.getLong("id");
        Assert.notNull(id,"记录Id不能为空");
        BbsPost bbsPost = bbsPostService.getById(id);
        LhtUser lhtUser = lhtUserService.getById(bbsPost.getUserId());
        BbsPostVo result =  new BbsPostVo();
        BeanUtils.copyProperties(bbsPost,result);
        result.setUserPhone(lhtUser.getPhone());

        List<BbsPostMedia> mediaList = bbsPostMediaService.selectListByPostId(id);
        if(mediaList != null && mediaList.size() > 0){
            String collect = mediaList.stream()
                    .map(BbsPostMedia::getMedia)
                    .collect(Collectors.joining(","));
            result.setFiles(collect);
        }
        return result;
    }

}
